package com.zhinan.common;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

/**
 * @Description: 统一返回结果实体类
 * @Author: ZhiNan
 * @Date: 2023-04-13 14:26
 * @Version: 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R implements Serializable {

    private static final long serialVersionUID = 1L;


    private String code;


    private String msg;

    private Object data;

    private boolean flag;


    /**
     * 成功-自定义成功时返回信息和数据
     */
    public static R success(String msg, Object data) {
        return new R(SysConstant.SUCCESS_CODE, msg, data, true);
    }

    /**
     * 成功-自定义成功时只需返回数据
     */
    public static R success(Object data) {
        return new R(SysConstant.SUCCESS_CODE, SysConstant.SUCCESS_MESSAGE, data, true);
    }

    /**
     * 成功-自定义状态码和消息
     */
    public static R success(String code, String msg) {
        return new R(code, msg, SysConstant.BLANK_STRING, true);
    }

    /**
     * 失败-自定义状态码,失败时信息
     */

    public static R fail(String code, String msg) {
        return new R(code, msg, SysConstant.BLANK_STRING, false);
    }

    /**
     * 失败-通用失败返回方法
     */

    public static R fail() {
        return new R(SysConstant.ERROR_CODE, SysConstant.ERROR_MESSAGE, SysConstant.BLANK_STRING, false);
    }


}

